package item.lxp.Interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Component
public class TimeInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        HandlerMethod handlerMethod = (HandlerMethod) handler;

        request.setAttribute("name","123");

        System.out.println("拦截器开始。。。");

        System.out.println("拿不到参数。。。");

        System.out.println("获取Bean :" + handlerMethod.getBean());

        System.out.println("获取访问名 " + handlerMethod.getMethod().getName());


        // 返回 false 此处不在进入 controller 层
        return true;
    }


    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerMethod handlerMethod = (HandlerMethod) handler;

        String value =  request.getAttribute("name").toString();

        System.out.println("拦截器结束。。。");

        System.out.println("拿到的拦截器开始的参数 ：" + value );


    }


    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("异常。。。");
//        System.out.println("interceptor 异常处理:" + ex.getMessage());
    }
}
